package com.hoppinzq.test;

import org.htmlunit.WebClient;
import org.htmlunit.html.*;
import org.junit.jupiter.api.Test;
import org.springframework.util.Assert;

import java.util.List;

/**
 * @author ZhangQi
 */
public class demo4 {

    @Test
    public void homePage() throws Exception {
        try (final WebClient webClient = new WebClient()) {
            final HtmlPage page = webClient.getPage("http://hoppin.cn/");
            System.err.println(page.getTitleText());
//            final String pageAsXml = page.asXml();
//            System.out.println(pageAsXml);
//            final String pageAsText = page.asNormalizedText();
//            System.err.println(pageAsText);
        }
    }

    @Test
    public void submittingForm() throws Exception {
        final WebClient webClient = new WebClient();
        webClient.getOptions().setJavaScriptEnabled(true);
        webClient.getOptions().setThrowExceptionOnScriptError(false);
        //webClient.getOptions().setTimeout(5000);

        final HtmlPage page = webClient.getPage("http://150.158.28.40:8806/contact.html");

//            final HtmlForm form = page1.getFormByName("myform");
//
//            final HtmlSubmitInput button = form.getInputByName("submitbutton");
//            final HtmlTextInput textField = form.getInputByName("name");

        //textField.type("root");

        // Now submit the form by clicking the button and get back the second page.
        //final HtmlPage page2 = button.click();

        final HtmlTextInput textField = page.getHtmlElementById("name");
        final HtmlTextInput contact = page.getHtmlElementById("contact");
        final HtmlTextArea message = page.getHtmlElementById("message");
        textField.setValue("root");
        contact.setValue("yes");
        message.setText("ok");
        System.err.println(textField.getType());
        List<Object> byXPath = page.getByXPath("/html/body/div[3]/div/div[2]/div/div/div/div[4]/button");
        final HtmlButton button = (HtmlButton) byXPath.get(0);
        button.click();
    }
}

